package com.jcark.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.annotation.StyleRes
import com.jcark.ui.R
import com.jcark.ui.dialog.style.IMutableDialogStyle

/**
 * Description: 对话框可变样式的对话框<br>
 *
 * @author King.Zi<br>
 * @date 2022/6/14<br>
 * Email: 78688689@qq.com
 */
class MutableStyleDialog(context: Context, @StyleRes themeResId: Int, mutableDialogStyle: IMutableDialogStyle) :
    Dialog(context, themeResId) {
    private val mTitleTextView: TextView
    private val mContentTextView: TextView
    private val mLeftButtonView: TextView
    private val mRightButtonView: TextView
    private val mSplitLineView: View
    private var mContext: Context? = null

    init {
        mContext = context
        val view = View.inflate(context, R.layout.dialog_mutable, null)
        mTitleTextView = view.findViewById(R.id.title_tv)
        mContentTextView = view.findViewById(R.id.content_tv)
        mRightButtonView = view.findViewById(R.id.right_tv)
        mLeftButtonView = view.findViewById(R.id.left_tv)
        mSplitLineView = view.findViewById(R.id.split_line)
        mRightButtonView.tag = this
        mLeftButtonView.tag = this
        setContentView(view)

        //set style
        mutableDialogStyle?.let {
            setTextAppearance(mTitleTextView, it.titleTextStyle)
            setTextAppearance(mContentTextView, it.contentTextStyle)

            setTextAppearance(mLeftButtonView, it.leftButtonTextStyle)
            setBackground(mLeftButtonView, it.getLeftButtonBackground(mContext))

            setTextAppearance(mRightButtonView, it.rightButtonTextStyle)
            setBackground(mRightButtonView, it.getRightButtonBackground(mContext))
        }

        mLeftButtonView.setOnClickListener { dismiss() }

        val dm = context.resources.displayMetrics
        val screenWidth = dm.widthPixels
        // 0.75 ： 进行设置在屏幕中显示的宽度
        window!!.attributes.width = (screenWidth * 0.75).toInt()
    }

    /**
     * 设置背景
     * @param view 控件
     * @param drawable 背景，为空时不设置
     */
    private fun setBackground(view: View, drawable: Drawable?) {
        if (null != drawable) {
            view.background = drawable
        }
    }

    @SuppressLint("ResourceType")
    private fun setTextAppearance(textView: TextView, @StyleRes resId: Int) {
        if (resId > 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                textView.setTextAppearance(resId)
            } else {
                textView.setTextAppearance(mContext, resId)
            }
        }
    }

    /**
     * 设置按钮文字
     *
     * @param textView
     * @param buttonText
     */
    private fun setButtonText(textView: TextView, buttonText: String?) {
        if (TextUtils.isEmpty(buttonText)) {
            textView.visibility = View.GONE
            mSplitLineView.visibility = View.GONE
        } else {
            textView.text = buttonText
        }
    }

    /**
     * 设置标题或内容文本
     * @param textView 文本控件
     * @param text 文本
     */
    private fun setText(textView: TextView, text: CharSequence?) {
        if (TextUtils.isEmpty(text)) {
            textView.visibility = View.GONE
        } else {
            textView.text = text
        }
    }

    /**
     * 设置标题，标题为空时不显示
     * @param text 标题
     */
    fun setTitle(@Nullable text: String?) {
        setText(mTitleTextView, text)
    }

    /**
     * 设置内容，内容为空时不显示
     * @param text 内容
     */
    fun setContentText(@Nullable text: CharSequence?) {
        setText(mContentTextView, text)
    }

    /**
     * 设置左侧按钮，按钮文本为空时不显示
     * @param text 按钮文本
     * @param clickListener 点击事件
     */
    fun setLeftButton(@Nullable text: String?, @Nullable clickListener: View.OnClickListener?) {
        setButtonText(mLeftButtonView, text)
        if (null != clickListener) {
            mLeftButtonView.setOnClickListener(clickListener)
        }
    }

    /**
     * 设置右侧按钮，按钮文本为空时不显示
     * @param text 按钮文本
     * @param clickListener 点击事件
     */
    fun setRightButton(@Nullable text: String?, @Nullable clickListener: View.OnClickListener?) {
        setButtonText(mRightButtonView, text)
        if (null != clickListener) {
            mRightButtonView.setOnClickListener(clickListener)
        }
    }

    /**
     * 设置标题字体大小(单位sp)
     */
    fun setTitleSize(size: Float) {
        mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size)
    }

    /**
     * 获取标题控件
     */
    fun getTitleView(): TextView {
        return mTitleTextView
    }

    /**
     * 获取内容控件
     */
    fun getContentView(): TextView {
        return mContentTextView
    }

    /**
     * 获取右按钮控件
     */
    fun getLeftButton(): TextView {
        return mLeftButtonView
    }

    /**
     * 获取右按钮控件
     */
    fun getRightButton(): TextView {
        return mRightButtonView
    }
}